import { createRouter, createWebHistory } from 'vue-router'
import LoginPage from '@/views/login/LoginPage.vue'
import LayoutContainer from '@/views/layout/LayoutContainer.vue'
import ArticleManage from '@/views/aricle/ArticleManage.vue'
import ArticleChannel from '@/views/aricle/ArticleChannel.vue'
import UserProfile from '@/views/user/UserProfile.vue'
import UserAvatar from '@/views/user/UserAvatar.vue'
import UserPassword from '@/views/user/UserPassword.vue'
import { useUserStore } from '@/stores/userStore'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: LoginPage },
    {
      path: '/',
      component: LayoutContainer,
      children: [
        { path: '/article/manage', component: ArticleManage },
        { path: '/article/channel', component: ArticleChannel },
        { path: '/user/profile', component: UserProfile },
        { path: '/user/avatar', component: UserAvatar },
        { path: '/user/password', component: UserPassword }
      ]
    }
  ]
})
// 登录访问拦截
// to是要到达的页面 from是来的页面
router.beforeEach((to) => {
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') return '/login'
})
export default router
